package com.example.lcpharmacy.service;

import com.example.lcpharmacy.entity.ReceiverEntity;
import com.example.lcpharmacy.entity.reqBean.ReceiverBean;
import com.example.lcpharmacy.mapper.ReceiverDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReceiverService {

    @Autowired
    private ReceiverDao receiverDao;

    public int saveRece(ReceiverBean receiverBean) {
        List<String> cityDetail = receiverBean.getCitys();

        String provinceName = null;
        String cityName = null;
        //省 + 市 的情况
        if (cityDetail.size() > 1) {
            provinceName = cityDetail.get(0);
            cityName = cityDetail.get(1);
            //直辖市的情况
        } else if (cityDetail.size() != 0) {
            provinceName = cityDetail.get(0);
            cityName = cityDetail.get(0);
        }

        ReceiverEntity receiverEntity = new ReceiverEntity();
        receiverEntity.setUserName(receiverBean.getUserName());
        receiverEntity.setReceiverPhone(receiverBean.getPhone());
        receiverEntity.setReceiverProvinceName(provinceName);
        receiverEntity.setReceiverCityName(cityName);
        receiverEntity.setReceiverAddress(receiverBean.getAddress());

        Integer receiverNumber = receiverDao.queryCount(receiverEntity);
        if (receiverNumber == 0) {
            int result = receiverDao.insertReceInfo(receiverEntity);
            return result;
        } else {
            int result = receiverDao.updateByName(receiverEntity);
            return result;
        }
    }
}
